package com.yuantong.xyerp.core.provider.service.base;

import com.alibaba.druid.util.StringUtils;
import com.yuantong.xyerp.core.api.model.Area;
import com.yuantong.xyerp.core.api.service.IAreaService;
import com.yuantong.xyerp.core.api.vo.AreaFullVo;
import com.yuantong.xyerp.core.provider.dao.AreaMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AreaServiceImpl implements IAreaService {
    @Resource
    private AreaMapper areaMapper;

    private Map<String, Area> areaMap;

    @PostConstruct
    private void init() {
        areaMap = areaMapper
                .findAll()
                .stream()
                .collect(Collectors.toMap(Area::getCode, a -> a));
    }

    @Override
    public AreaFullVo getFullArea(String areaCode) {
        if (StringUtils.isEmpty(areaCode)) {
            return null;
        }

        AreaFullVo vo = new AreaFullVo();

        if (!areaCode.endsWith("00")) {
            Area district = areaMap.get(areaCode);
            if (district == null) {
                return null;
            }
            vo.setDistrictCode(district.getCode());
            vo.setDistrictName(district.getName());
        }
        if (!areaCode.endsWith("0000")) {
            Area city = areaMap.get(areaCode.substring(0, 4) + "00");
            if (city == null) {
                return null;
            }
            vo.setCityCode(city.getCode());
            vo.setCityName(city.getName());
        }

        Area province = areaMap.get(areaCode.substring(0, 2) + "0000");
        if (province == null) {
            return null;
        }
        vo.setProvinceCode(province.getCode());
        vo.setProvinceName(province.getName());
        return vo;
    }

    @Override
    public List<Area> listProvinces() {
        return areaMap
                .values()
                .stream()
                .filter(area -> area.getCode().endsWith("0000"))
                .collect(Collectors.toList());
    }

    @Override
    public List<Area> listCities(String provinceCode) {
        String codePrefix = provinceCode.substring(0, 2);
        return areaMap
                .values()
                .stream()
                .filter(area -> area.getCode().startsWith(codePrefix) && area.getCode().endsWith("00") && !area.getCode().endsWith("0000"))
                .collect(Collectors.toList());
    }

    @Override
    public List<Area> listDistricts(String cityCode) {
        String codePrefix = cityCode.substring(0, 4);
        return areaMap
                .values()
                .stream()
                .filter(area -> area.getCode().startsWith(codePrefix) && !area.getCode().endsWith("00"))
                .collect(Collectors.toList());
    }

    @Override
    public List<String> findAreaNameByCodes(List<String> codes) {
        return areaMapper.findAreaNameByCodes(codes);
    }
}
